package com.example.sfzreader.utils;

import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.util.Log;

import com.example.sfzreader.receiver.UsbReceiver;

import java.util.HashMap;

public class UsbDevPermission {

    private static String TAG = "UsbDevPermission";
    private static UsbManager mUsbManager;
    private static UsbDevice mUsbDevice;
    // private static UsbInterface mInterface;
    private static UsbDeviceConnection mDeviceConnection;
    private static PendingIntent mPermissionIntent;
    private static Context mContext;

    private static String usbDevicePath = "";
    private UsbReceiver mUsbReceiver;

    // 注1：UsbManager.ACTION_USB_DEVICE_ATTACHED对应的广播在USB每次插入时都能监听到，所以用这个就可以监听USB插入。
    // 注2：UsbManager.ACTION_USB_DEVICE_DETACHED用来监听USB拔出广播。

    public UsbDevPermission(Context context) {
        mContext = context;
        mPermissionIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("com.hdos.usbdevice.UsbDeviceLib.USB_PERMISSION"), 0);
        // 注册USB设备权限管理广播
        IntentFilter filter = new IntentFilter("com.hdos.usbdevice.UsbDeviceLib.USB_PERMISSION");
        filter.addAction("com.android.example.USB_PERMISSION");
        filter.addAction("android.hardware.usb.action.USB_DEVICE_ATTACHED");
        filter.addAction("android.hardware.usb.action.USB_DEVICE_DETACHED");
        if (mUsbReceiver == null) {
            mUsbReceiver = new UsbReceiver();
        }
        mContext.registerReceiver(mUsbReceiver, filter);
    }

    public void unUsbDevPermission() {
        if (mContext != null && mUsbReceiver != null) {
            mContext.unregisterReceiver(mUsbReceiver);
        }
    }


    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    public int getUsbFileDescriptor(int VendorId, int ProductID) {
        int UsbFileDescriptor = 0;
        mUsbManager = ((UsbManager) mContext.getSystemService("usb"));
        HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
        if (deviceList.size() <= 0) {
            return -RetCode.ERR_NOT_FOUND_USBDEVICE;
        }
        if (!deviceList.isEmpty()) {
            for (UsbDevice device : deviceList.values()) {
                usbDevicePath = device.getDeviceName();
                if ((device.getVendorId() == VendorId) && (device.getProductId() == ProductID)) {
                    mUsbDevice = device;
                    // 判断下设备权限，如果没有权限，则请求权限
                    if (!mUsbManager.hasPermission(mUsbDevice)) {
                        mUsbManager.requestPermission(mUsbDevice, mPermissionIntent);
                    }
                }
            }
        }
		/*
		if (mUsbDevice != null) {
			Log.d(TAG, "interfaceCounts : " + mUsbDevice.getInterfaceCount());
			for (int i = 0; i < mUsbDevice.getInterfaceCount(); i++) {
				UsbInterface intf = mUsbDevice.getInterface(i);
				Log.d(TAG,"interface class =" + intf.getInterfaceClass() + " ;InterfaceSubclass="+ intf.getInterfaceSubclass() + " ;InterfaceProtocol=" + intf.getInterfaceProtocol());
				if ((intf.getInterfaceClass() == 3) && (intf.getInterfaceSubclass() == 0)&& (intf.getInterfaceProtocol() == 0)) {
					mInterface = intf;
					break;
				}
			}
		}
		if (mInterface != null)
		*/
        if (mUsbDevice != null) {
            UsbDeviceConnection conn = null;
            if (mUsbManager.hasPermission(mUsbDevice)) {
                Log.d(TAG, "has permission");
                conn = mUsbManager.openDevice(mUsbDevice);
            } else {
                Log.d(TAG, "no permission");
                return -RetCode.ERR_USB_NOPERMISSION;
            }
            if (conn == null) {
                return -RetCode.ERR_USB_CANNOT_CONNTECT;
            }
            mDeviceConnection = conn;
            UsbFileDescriptor = conn.getFileDescriptor();
            Log.d(TAG, " getFileDescriptor is " + UsbFileDescriptor);
			/*
			if (conn.claimInterface(mInterface, true)) {
				mDeviceConnection = conn;
				UsbFileDescriptor = conn.getFileDescriptor();
				Log.d(TAG, "mtReaderDev FileDescriptor is " + UsbFileDescriptor);
			} else {
				conn.close();
			}
			boolean isOp = isOpen();
			return isOp;
			*/
            return UsbFileDescriptor;
        }
        return -RetCode.ERR_NOT_FOUND_USBDEVICE;
    }

    public String getUsbDevPath() {
        return usbDevicePath;
    }

    public boolean isOpen() {
        if ((mUsbManager != null) && (mUsbDevice != null)
                /* && (mInterface != null) */
                && (mDeviceConnection != null) && (mUsbManager.hasPermission(mUsbDevice))) {
            return true;
        }
        return false;
    }
}
